package com.qianxun.module.datasource1.develop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianxun.module.datasource1.develop.entity.SysFunctionRoleRelation;
import com.qianxun.module.datasource1.develop.manager.SysFunctionRoleRelationCacheManager;
import com.qianxun.module.datasource1.develop.mapper.SysFunctionRoleRelationMapper;
import com.qianxun.module.datasource1.develop.service.SysFunctionRoleRelationService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author admin
 * @since 2019-11-27
 */
@Service
public class SysFunctionRoleRelationServiceImpl extends ServiceImpl<SysFunctionRoleRelationMapper, SysFunctionRoleRelation> implements SysFunctionRoleRelationService {

    @Resource
    @Lazy
    private SysFunctionRoleRelationCacheManager sysFunctionRoleCacheManager;

    @Override
    public void removeByFunctionRoleId(Integer functionRoleId) {
        this.baseMapper.deleteByFunctionRoleId(functionRoleId);
    }

    @Override
    public void removeByFunctionRoleIds(List<Integer> functionRoleIdList) {
        this.baseMapper.deleteByFunctionRoleIdList(functionRoleIdList);
    }

    @Override
    public List<SysFunctionRoleRelation> listByFunctionRoleId(Integer functionRoleId) {
       return this.baseMapper.selectListByFunctionRoleId(functionRoleId);
    }

    @Override
    public void setAuthority(Integer functionRoleId, Integer[] functionIds) {
        //删除已存在的映射关系
        sysFunctionRoleCacheManager.removeByFunctionRoleId(functionRoleId);

        List<SysFunctionRoleRelation> list = new ArrayList<>(functionIds.length);
        for(int i = 0 ;i < functionIds.length ;++i){
            if(functionIds[i] == 0){
                continue;
            }
            SysFunctionRoleRelation sysAdminFunctionRoleRelation = new SysFunctionRoleRelation();
            sysAdminFunctionRoleRelation.setFunctionRoleId(functionRoleId).setFunctionId(functionIds[i]);
            sysAdminFunctionRoleRelation.init();
            list.add(sysAdminFunctionRoleRelation);
        }
        saveBatch(list);
    }

    @Override
    public List<Integer> listFunctionIdByFunctionRoleId(Integer functionRoleId) {
        return this.baseMapper.selectFunctionIdByFunctionRoleId(functionRoleId);
    }

    @Override
    public List<String> listFunctionNameByFunctionId(Long functionId) {
        return sysFunctionRoleCacheManager.listFunctionNameByFunctionId(functionId);
    }
}
